package com.zuoban.log;

import com.zuoban.entity.sys.SysLog;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Optional;

/**
 * @BelongsProject: zuoban-boot
 * @BelongsPackage: com.zuoban.log
 * @Author: zxy
 * @CreateTime: 2022-10-10
 * @Description: ...
 */
@Component
@RequiredArgsConstructor
public class SubjectImpl implements Subject {

    private final Map<String,Observer> map;

    @Override
    public void addObserver(Observer observer) {
        map.put(observer.getClass().getAnnotation(Component.class).value(),observer);
    }

    @Override
    public void delObserver(Observer observer) {
        map.remove(observer.getClass().getAnnotation(Component.class).value(),observer);
    }

    @Override
    public void notifyObserver(HttpServletRequest request, Observer observer, SysLog sysLog) {
        observer = Optional.ofNullable(observer).orElse(new LogObserverImpl());
        map.get(observer.getClass().getAnnotation(Component.class).value()).addLog(request,sysLog);
    }

}
